package sg.edu.nus.iss.billing.vo.customer;

import sg.edu.nus.iss.billing.common.DbUtil;
import sg.edu.nus.iss.billing.common.exception.InvalidParamsException;
import sg.edu.nus.iss.billing.vo.common.IDbSerialisable;

public class AddressVO implements IDbSerialisable {

	private String line1;
	private String line2;
	private String postalCode;
	private String city;
	private String state;
	private String country;
	private String addresseeNric;
	/**
	 * @return the line1
	 */
	public String getLine1() {
		return line1;
	}
	/**
	 * @param line1 the line1 to set
	 */
	public void setLine1(String line1) {
		this.line1 = line1;
	}
	/**
	 * @return the line2
	 */
	public String getLine2() {
		return line2;
	}
	/**
	 * @param line2 the line2 to set
	 */
	public void setLine2(String line2) {
		this.line2 = line2;
	}
	/**
	 * @return the state
	 */
	public String getState() {
		return state;
	}
	/**
	 * @param state the state to set
	 */
	public void setState(String state) {
		this.state = state;
	}
	/**
	 * @return the country
	 */
	public String getCountry() {
		return country;
	}
	/**
	 * @param country the country to set
	 */
	public void setCountry(String country) {
		this.country = country;
	}
	/**
	 * @return the postalCode
	 */
	public String getPostalCode() {
		return postalCode;
	}
	/**
	 * @param postalCode the postalCode to set
	 */
	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}
	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}
	/**
	 * @param city the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}
	
	public String addressAsSingleLine(){
		
		StringBuffer sb = new StringBuffer();
		sb.append(line1+" "+line2);
		sb.append(" ,S(");
		sb.append(postalCode+"),");
		sb.append(","+city+","+state+","+country+".");
		String fullAddress = sb.toString();
		return fullAddress;
	}
	@Override
	public void setParameters(String serialisedEntry)
			throws InvalidParamsException {
		String[] tokens =  serialisedEntry.split(java.util.regex.Pattern.quote(DbUtil.DELIMITER));
		if (tokens.length != 7) {
			throw new InvalidParamsException("Expected 5 parameters, received " + tokens.length);
		}
		else {
			setAddresseeNric(tokens[0]);
			setLine1(tokens[1]);
			setLine2(tokens[2]);
			setPostalCode(tokens[3]);
			setCity(tokens[4]);
			setState(tokens[5]);
			setCountry(tokens[6]);
		}
		
	}
	@Override
	public String serialised() {
		// addresseeNric^line1^line2^postalCode^city^state^country
		StringBuilder sb = new StringBuilder();
		sb.append(getAddresseeNric());
		sb.append(DbUtil.DELIMITER);
		sb.append(getLine1());
		sb.append(DbUtil.DELIMITER);
		sb.append(getLine2());
		sb.append(DbUtil.DELIMITER);
		sb.append(getPostalCode());
		sb.append(DbUtil.DELIMITER);
		sb.append(getCity());
		sb.append(DbUtil.DELIMITER);
		sb.append(getState());
		sb.append(DbUtil.DELIMITER);
		sb.append(getCountry());
		return sb.toString();
	}
	/**
	 * @return the addresseeNric
	 */
	public String getAddresseeNric() {
		return addresseeNric;
	}
	/**
	 * @param addresseeNric the addresseeNric to set
	 */
	public void setAddresseeNric(String addresseeNric) {
		this.addresseeNric = addresseeNric;
	}
}
